package org.eclipse.php.internal.console.ui.view;

import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IDebugEventSetListener;
import org.eclipse.debug.core.model.IStackFrame;
import org.eclipse.debug.core.model.IThread;
import org.eclipse.debug.internal.ui.views.variables.VariablesView;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.php.internal.console.ui.editor.MyRunnable;

/**
 * This sample class demonstrates how to plug-in a new workbench view. The view
 * shows data obtained from the model. The sample creates a dummy model on the
 * fly, but a real implementation would connect to the model available either in
 * this or another plug-in (e.g. the workspace). The view is connected to the
 * model using a content provider.
 * <p>
 * The view uses a label provider to define how model objects should be
 * presented in the view. Each view can present the same model objects using
 * different labels and icons, if needed. Alternatively, a single label provider
 * can be shared between views in order to ensure that objects of the same type
 * are presented in the same way everywhere.
 * <p>
 */

public class PHPConsoleVariablesView extends VariablesView implements
		IDebugEventSetListener {
	public static final String ID="org.eclipse.php.console.views.PHPConsoleVariablesView";
	public static PHPConsoleVariablesView instance;

	/**
	 * The constructor.
	 */
	public PHPConsoleVariablesView() {
		instance=this;
		DebugPlugin.getDefault().addDebugEventListener(this);
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();
		DebugPlugin.getDefault().removeDebugEventListener(this);
	}

	public static void contextActivated(Object o) {
		if (instance != null)
			instance.contextActivated(new StructuredSelection(o));
	}

	protected void contextActivated(ISelection selection) {
		if (!isAvailable() || !isVisible()) {
			return;
		}
		if (selection instanceof IStructuredSelection) {

			selection=toPHPStackFrame((IStructuredSelection) selection);
			if (selection != null)
				super.contextActivated(selection);
		}
	}

	@Override
	protected void becomesVisible() {
		super.becomesVisible();
		activatePHPStackFrame();
	}

	// public void clearMemory() {
	// this.symbolTable.clear();
	// }

	private ISelection toPHPStackFrame(IStructuredSelection selection) {
		StructuredSelection result=null;
		if (MyRunnable.launch != null
				&& MyRunnable.launch.getDebugTarget() != null) {
			IThread[] threads;
			try {
				threads=MyRunnable.launch.getDebugTarget().getThreads();
				for (int j=0; j < threads.length; j++) {
					IStackFrame sf=threads[j].getTopStackFrame();
					if (sf != null)
						result=new StructuredSelection(sf);
				}
			} catch (DebugException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			result=new StructuredSelection();
		}

		// Object source = ((IStructuredSelection)selection).getFirstElement();
		// if(source instanceof PHPDebugTarget ){
		// if(source == ConsoleEditor.launch.getDebugTarget()){
		//				
		//				
		// }
		// }else if(source instanceof PHPStackFrame){
		// if(((PHPStackFrame)source).getDebugTarget() ==
		// ConsoleEditor.launch.getDebugTarget())
		// return selection;
		// }
		return selection;
	}

	@Override
	public void handleDebugEvents(DebugEvent[] events) {
		// System.out.println(events[0].getSource());
		activatePHPStackFrame();

	}

	public static void activatePHPStackFrame() {
		if (MyRunnable.launch != null) {
			IThread[] threads;
			try {
				threads=MyRunnable.launch.getDebugTarget().getThreads();
				for (int j=0; j < threads.length; j++) {
					IStackFrame sf=threads[j].getTopStackFrame();
					if (sf != null) {
						contextActivated(sf);
						return;
					}

				}
			} catch (DebugException e) {
				e.printStackTrace();
			}

		}
	}
}